package com.fdb.efp.esb.service.impl.yed;

import com.fdb.efp.esb.common.client.resp.EsbRespRetInfBean;
import com.fdb.efp.esb.common.client.resp.EsbRespServiceBean;
import com.fdb.efp.esb.common.client.resp.EsbRespSysHeadBean;
import com.fdb.efp.esb.common.constant.ESBClientConstance;
import com.fdb.basic.framework.core.constant.ESBConstant;
import com.fdb.efp.esb.service.bo.resp.yed.YedLoanReleaseReqBean;
import com.fdb.efp.esb.service.bo.resp.yed.YedLoanReleaseRespBean;
import com.fdb.efp.esb.service.client.ESBRequestBuilder;
import com.fdb.efp.esb.service.client.EsbBaseBean;
import com.fdb.efp.esb.service.facade.yed.YedHjLoanReleaseService;
import com.fdb.efp.nls.common.exception.LoanReleaseException;
import com.fdb.efp.nls.service.vo.NlsApplyInfoVO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.Optional;

@Service("yedHjLoanReleaseService")
public class YedHjLoanReleaseServiceImpl implements YedHjLoanReleaseService {
    private static Logger logger = LoggerFactory.getLogger(YedHjLoanReleaseServiceImpl.class);
	/**
	 * 消息模板
	 */
	private final String msgTemplate = "message{ }";


    @Override
    public YedLoanReleaseRespBean applyPsdLoanRelease(YedLoanReleaseReqBean psdLoanReleaseReqBean, NlsApplyInfoVO nlsApplyInfoVO) throws LoanReleaseException {
        logger.info("======>优e贷调用贷款开立发放接口【" + nlsApplyInfoVO.getApplySeq() + "_30210001_04】开始<======");
        YedLoanReleaseRespBean psdLoanReleaseRespBean = null;
		EsbRespServiceBean recvBean = null;
        String retCode;
        try {
            EsbBaseBean esbBaseBean = EsbBaseBean.EsbBaseBeanBuilder.
                    anEsbBaseBean().
					withGLBLSRLNO(nlsApplyInfoVO.getGlobalSerno()).
					withINSTID(ESBClientConstance.bdInstId).withPLAFMUSRID(nlsApplyInfoVO.getPlatfmUsrID()).withPLATFMID(nlsApplyInfoVO.getPlatfmId()).build();
           recvBean = ESBRequestBuilder.buildClient().
                    withEsbBaseBean(esbBaseBean)
                    .withBody(psdLoanReleaseReqBean)
                    .withTradeNo(ESBConstant.TRADE_CODE_30210001)
                    .withScene(ESBConstant.SCENE_NO_04)
                    .create()
                    .sendAndReceive();
        } catch (Exception e) {
            logger.error("优e贷调用贷款开立发放接口【30210001_04】出现异常：" + e.getMessage());
			e.printStackTrace();
			throw new LoanReleaseException("请求贷款开立发服务接口服务接口调用失败【" + nlsApplyInfoVO.getApplySeq() + "_30210001_04】"+e);
        }
        EsbRespSysHeadBean sysHead =
				recvBean.getSysHead();
		if (Objects.nonNull(sysHead)) {
			// 返回信息数据不为空
			EsbRespRetInfBean esbRespRetInfBean = sysHead.getRetInfArry().stream().findFirst().orElseGet(EsbRespRetInfBean::new);
			retCode = Optional.ofNullable(esbRespRetInfBean.getRetCd()).orElse(StringUtils.EMPTY);
			if (ESBConstant.SUCCESS.equals(retCode)) {
				String msg2 = "++++====>请求贷款开立发服务接口调用成功<====++++";
				psdLoanReleaseRespBean = recvBean.getBody(YedLoanReleaseRespBean.class);
				logger.debug(msg2, msgTemplate);
			} else{
				String msg3 = esbRespRetInfBean.getRetMsg();
				logger.error(msg3);
				throw new LoanReleaseException(msg3);
			}
		}else {
			if (logger.isErrorEnabled()){
				logger.error("请求贷款开立发服务接口【" + nlsApplyInfoVO.getApplySeq() + "_30210001_04】 获取的全局流水号为空");
			}
			throw new LoanReleaseException("请求贷款开立发服务接口【" + nlsApplyInfoVO.getApplySeq() + "__30210001_04】 获取的全局流水号为空");
		}
		logger.info("======>调用贷款开立发服务接口【"+ nlsApplyInfoVO.getApplySeq() +"_30210001_04】结束<======");
        return psdLoanReleaseRespBean;
    }

}